<?php

// +----------------------------------------------------------------------
// | ThinkPHP
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$



/**
 +------------------------------------------------------------------------------
 * Http 工具类
 * 提供一系列的Http方法
 +------------------------------------------------------------------------------
 * @category   ORG
 * @package  ORG
 * @subpackage  Net
 * @author    liu21st <liu21st@gmail.com>
 * @version   $Id$
 +------------------------------------------------------------------------------
 */
class Http extends Base
{ 
    /**
     +----------------------------------------------------------
     * 显示HTTP Header 信息
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     */
    static function get_header_info($header = '', $echo = true)
    {
        ob_start();
        $headers = getallheaders();
        if (! empty($header)) {
            $info = $headers[$header];
            echo ($header . ':' . $info . "\n");
            ;
        }
        else {
            foreach($headers as $key => $val) {
                echo ("$key:$val\n");
            }
        }
        $output = ob_get_clean();
        if ($echo) {
            echo (nl2br($output));
        }
        else {
            return $output;
        }
    
    }

    /**
     * HTTP Protocol defined status codes
     * @param int $num
     */
    static function send_http_status($code)
    {
        static $_status = array(
            // Informational 1xx
        100 => 'Continue', 101 => 'Switching Protocols', 

        // Success 2xx
        200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 

        // Redirection 3xx
        300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', // 1.1
303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', // 306 is deprecated but reserved
307 => 'Temporary Redirect', 

        // Client Error 4xx
        400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', 

        // Server Error 5xx
        500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', 509 => 'Bandwidth Limit Exceeded' 
        );
        
        if (array_key_exists($code, $_status)) {
            header('HTTP/1.1 ' . $code . ' ' . $_status[$code]);
        }
    }

    /**
     * Access values contained in the superglobals as public members
     * Order of precedence: 1. GET, 2. POST, 3. COOKIE, 4. SERVER, 5. ENV
     *
     * @see http://msdn.microsoft.com/en-us/library/system.web.httprequest.item.aspx
     * @param string $key
     * @return mixed
     */
    protected function __get($key)
    {
        switch(TRUE)
        {
            case isset($this->_params[$key]):
                return $this->_params[$key];
            case isset($_GET[$key]):
                return $_GET[$key];
            case isset($_POST[$key]):
                return $_POST[$key];
            case isset($_COOKIE[$key]):
                return $_COOKIE[$key];
            case ($key == 'REQUEST_URI'):
                return $this->getRequestUri();
            case isset($_SERVER[$key]):
                return $_SERVER[$key];
            case isset($_ENV[$key]):
                return $_ENV[$key];
            default:
                return NULL;
        }
    }

    /**
     * Set values
     *
     * In order to follow {@link __get()}, which operates on a number of
     * superglobals, setting values through overloading is not allowed and will
     * raise an exception. Use setParam() instead.
     *
     * @param string $key
     * @param mixed $value
     * @return void
     * @throws Wee_Request_Exception
     */
    protected function __set($key, $value)
    {
        throw new Wee_Request_Exception('Setting values in superglobals not allowed; please use setParam()');
    }

    /**
     * Check to see if a property is set
     *
     * @param string $key
     * @return boolean
     */
    protected function __isset($key)
    {
        switch(TRUE)
        {
            case isset($this->_params[$key]):
                return TRUE;
            case isset($_GET[$key]):
                return TRUE;
            case isset($_POST[$key]):
                return TRUE;
            case isset($_COOKIE[$key]):
                return TRUE;
            case isset($_SERVER[$key]):
                return TRUE;
            case isset($_ENV[$key]):
                return TRUE;
            default:
                return FALSE;
        }
    }

    /**
     * object to string
     * @return string   return original URL.
     */
    protected function __toString()
    {
        return $this->_requestUri;
    }

    /**
     * object to string
     * @return string   return original URL.
     */
    public function toString()
    {
        return $this->__toString();
    }

    /**
     * Alias to __get
     *
     * @param string $key
     * @return mixed
     */
    public function get($key)
    {
        return $this->__get($key);
    }

    /**
     * Alias to __set()
     *
     * @param string $key
     * @param mixed $value
     * @return void
     */
    public function set($key, $value)
    {
        return $this->__set($key, $value);
    }

    /**
     * Alias to __isset()
     *
     * @param string $key
     * @return boolean
     */
    public function has($key)
    {
        return $this->__isset($key);
    }


} 
?>